//
//  ZKShareView.m
//  Homept_ios
//
//  Created by pro on 16/9/22.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ZKShareView.h"

// view
#import "ZKBombView.h"
// 定义常量
static CGFloat titleHeight  = 40;
static CGFloat imageHeight  = 40;
static CGFloat textHeight   = 30;
static CGFloat buttonHeight = 40;

@implementation ZKShareCell

- (instancetype)initWithFrame:(CGRect)frame {
    
    if ([super initWithFrame:frame]) {
        NSLog(@"cell.frame:%@", NSStringFromCGRect(frame));
        // 创建图片
        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, imageHeight, imageHeight)];
        _imageView.userInteractionEnabled = YES;
        [self.contentView addSubview:_imageView];
       
        // 创建文字
        _textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.width, textHeight)];
        _textLabel.userInteractionEnabled = YES;
        _textLabel.font = FONT(12);
        _textLabel.textColor = [UIColor blackColor];
        _textLabel.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_textLabel];
        
        // 设置layout
        _imageView.x = self.width / 2;
        _textLabel.x = self.width / 2;
        
        CGFloat remain = (self.height - imageHeight - textHeight) / 2;
        _imageView.top = remain;
        _textLabel.top = _imageView.bottom;
        
    }
    return self;

}

@end


#pragma mark - *****************************
@implementation ZKShareView
{
    // view
    UICollectionView    *_collect;              // 列表
    UILabel             *_titleLabel;           // 标题
    UIButton            *_cancleButton;         // 取消
    
    // data
    NSArray             *_shareAry;             // 分享数组
}



- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.height = titleHeight + (imageHeight + textHeight + 10) + buttonHeight;
        _shareAry = @[@"QQ", /*@"微信", @"朋友圈"*/];
        self.backgroundColor = [UIColor colorWithWhite:0.98 alpha:0.9];
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat width = 0;
    CGFloat height = 0;
    // 创建titleLabel
    x = 0;
    y = 0;
    width = self.width;
    height = titleHeight;
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    _titleLabel.font = FONT(14);
    _titleLabel.text = @"分享";
    _titleLabel.textColor = [UIColor blackColor];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_titleLabel];
    
    // 创建cellect
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    if (_shareAry.count <= 5) {
        layout.itemSize = CGSizeMake(self.width / _shareAry.count, 10 + imageHeight + textHeight);
    } else {
        layout.itemSize = CGSizeMake(imageHeight + 10, 10 + imageHeight + textHeight);
    }
    
    x = 0;
    y = _titleLabel.bottom;
    width = self.width;
    height = 10 + imageHeight + textHeight;
    _collect = [[UICollectionView alloc] initWithFrame:CGRectMake(x, y, width, height) collectionViewLayout:layout];
    _collect.backgroundColor = [UIColor clearColor];
    [self addSubview:_collect];
    _collect.dataSource = self;
    _collect.delegate = self;
    _collect.scrollEnabled = NO;
    _collect.userInteractionEnabled = YES;
    //给集合视图注册一个类型的cell
    [_collect registerClass:[ZKShareCell class] forCellWithReuseIdentifier:@"cell"];
    
    // 创建按钮
    x = 0;
    y = _collect.bottom;
    width = self.width;
    height = buttonHeight;
    _cancleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _cancleButton.frame = CGRectMake(x, y, width, height);
    _cancleButton.backgroundColor = [UIColor whiteColor];
    _cancleButton.titleLabel.font = FONT(16);
    [_cancleButton setTitle:@"取消" forState:UIControlStateNormal];
    [_cancleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_cancleButton addTarget:self action:@selector(cancleAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_cancleButton];
}

#pragma mark - UICollectionViewDatasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _shareAry.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ZKShareCell *cell = (ZKShareCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.textLabel.text = _shareAry[indexPath.item];
    cell.imageView.image = [UIImage imageNamed:_shareAry[indexPath.item]];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString *shareText = @"住家分享";
    NSString *url = [BOYE_WEB_BASE_URL stringByAppendingString:@"share"];
    
    // 微信处理
    //    [UMSocialWechatHandler setWXAppId:WEIXIN_APPID appSecret:WEIXIN_APPSECRET url:url];
    //    [UMSocialQQHandler setQQWithAppId:QQ_APPID appKey:QQ_APPKEY url:url];
    
    // TODO: 分享链接的问题®
    // 微博分享的处理
    [UMSocialData defaultData].extConfig.sinaData.shareText = shareText;
    [UMSocialData defaultData].extConfig.sinaData.urlResource.url = url;
    [UMSocialData defaultData].extConfig.sinaData.urlResource.resourceType = UMSocialUrlResourceTypeImage;
    
    // 微信消息类型
    [UMSocialData defaultData].extConfig.wxMessageType = UMSocialWXMessageTypeWeb;
    
    // 微信朋友圈分享
    [UMSocialData defaultData].extConfig.wechatTimelineData.title = shareText;
    [UMSocialData defaultData].extConfig.wechatTimelineData.url = url;
    
    // 微信好友分享
    [UMSocialData defaultData].extConfig.wechatSessionData.title = shareText;
    [UMSocialData defaultData].extConfig.wechatSessionData.url = url;
    
    // qq分享
    [UMSocialData defaultData].extConfig.qqData.qqMessageType = UMSocialQQMessageTypeDefault;
    [UMSocialData defaultData].extConfig.qqData.title = shareText;
    [UMSocialData defaultData].extConfig.qzoneData.title = shareText;
    [UMSocialData defaultData].extConfig.qqData.url = url;
    [UMSocialData defaultData].extConfig.qzoneData.url = url;
    
    UIImageView *imageView  = [[UIImageView alloc] init];
    imageView.image = [UIImage imageNamed:@"default_head"];
    
    NSString *string = [_shareAry objectAtIndex:indexPath.row];
    
    
    if (indexPath.row == 0) { // qq分享
        
        [[UMSocialDataService defaultDataService] postSNSWithTypes:@[UMShareToQQ]
                                                           content:@"qq分享"
                                                             image:imageView.image//[UIImage getImageFromURL:self.model.main_img]
                                                          location:nil
                                                       urlResource:nil
                                               presentedController:self.viewController
                                                        completion:^(UMSocialResponseEntity *response) {
                                                            if (response.responseCode == UMSResponseCodeSuccess) {
                                                                NSLog(@"分享成功！");
                                                            }
                                                        }];
        
        
    } else if (indexPath.row == 1) { // 微信分享
        
        [[UMSocialDataService defaultDataService] postSNSWithTypes:@[UMShareToWechatSession]
                                                           content:@"微信分享"
                                                             image:imageView.image
                                                          location:nil
                                                       urlResource:nil
                                               presentedController:self.viewController
                                                        completion:^(UMSocialResponseEntity *response) {
                                                            if (response.responseCode == UMSResponseCodeSuccess) {
                                                                NSLog(@"分享成功！");
                                                            }
                                                        }];
        
        
    } else if (indexPath.row == 2) { // 朋友圈
        NSLog(@"%@分享", string);
        [[UMSocialDataService defaultDataService] postSNSWithTypes:@[UMShareToWechatTimeline]
                                                           content:@"朋友圈分享"
                                                             image:imageView.image
                                                          location:nil
                                                       urlResource:nil
                                               presentedController:self.viewController
                                                        completion:^(UMSocialResponseEntity *response) {
                                                            if (response.responseCode == UMSResponseCodeSuccess) {
                                                                NSLog(@"分享成功！");
                                                            }
                                                        }];
        
    }
    [self cancleAction];
}


#pragma mark - 点击
- (void)cancleAction {
    if (self.superview && [self.superview isKindOfClass:[ZKBombView class]]) {
        ZKBombView *supview = (ZKBombView *)self.superview;
        [supview remove];
    } else {
        NSLog(@"另外定义取消事件");
    }
}

@end
